<template>
  <el-form :model="form" label-width="120px">
    <el-form-item label="商品名称">
      <el-input v-model="form.name" />
    </el-form-item>

    <el-form-item label="商品图片">
      <el-input v-model="form.img" />
    </el-form-item>

    <el-form-item label="商品价格">
      <el-input v-model="form.price" />
    </el-form-item>

    <el-form-item label="是否推荐">
      <el-select v-model="form.isGood" placeholder="请选择">
        <el-option label="推荐" value="1" />
        <el-option label="不推荐" value="0" />
      </el-select>
    </el-form-item>


    <el-form-item>
      <el-button type="primary" @click="onSubmit">提交</el-button>
      <el-button type="primary" @click="onUpdate">修改</el-button>
      <el-button type="primary" @click="onDelete">删除</el-button>
      <el-button type="primary" @click="onFind">查询</el-button>
      <el-button>取消</el-button>
    </el-form-item>
  </el-form>
</template>

<script  setup>
import { ref } from 'vue'
import axios from 'axios'
// element引入文件
import { ElMessage } from 'element-plus'

// do not use same name with ref
const form = ref({
  name: '',
  img: '',
  price: '',
  isGood: '',
})

const onSubmit = () => {

  let url = "http://localhost:8091/item";
  axios.post(url,form.value).then(function (result){
    if(result.data.code === 200){
      ElMessage({
        message: result.data.message,
        type: 'success',
      })
    }else{
      ElMessage({
        message: result.data.message,
        type: 'error',
      })
    }
  })
}

const onUpdate = () => {
  let url = "http://localhost:8091/item";
  axios.put(url,form.value).then(function (result){
    if(result.data.code === 200){
      ElMessage({
        message: result.data.message,
        type: 'success',
      })
    }else{
      ElMessage({
        message: result.data.message,
        type: 'error',
      })
    }
  })
}

const onDelete = () => {
  let url = "http://localhost:8091/item/3";
  axios.delete(url).then(function (result){
    if(result.data.code === 200){
      ElMessage({
        message: result.data.message,
        type: 'success',
      })
    }else{
      ElMessage({
        message: result.data.message,
        type: 'error',
      })
    }
  })
}

const onFind = () => {
  let url = "http://localhost:8091/item";
  axios.get(url).then(function (result){
    console.log(result.data)
    if(result.data.code === 200){
      ElMessage({
        message: result.data.message,
        type: 'success',
      })
    }else{
      ElMessage({
        message: result.data.message,
        type: 'error',
      })
    }
  })
}
</script>

<style scoped>

</style>
